<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View; //模板引擎
use app\common\Base;
use think\facade\Request;
use app\model\Menus; //引入
use think\exception\ValidateException;
use app\validate\MenusVal;

/**
 * @name 菜单管理
 */
class MenusItem extends Base
{

    /**
     * @name 菜单列表
     */
    public function list()
    {
        $list = Menus::list();
        $arr = [];
        foreach ($list as $k => $v) {
            $v['action'] = $this->get_annotation('admin', $v['controller'], $v['action']);
            $v['controller'] = $this->get_class_annotation($v['controller'], 'admin');
            $arr[] = $v;
        }
        // dump($arr);
        View::assign('list', grades($arr));
        return View::fetch();
    }

    /**
     * @name 菜单添加
     */
    public function add()
    {
        $path = base_path() . 'Admin' . DIRECTORY_SEPARATOR . 'controller'; //控制器路径
        $pathList = glob($path . DIRECTORY_SEPARATOR . '*.php'); //模糊查询匹配
        $controllerList = [];
        foreach ($pathList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controllerList[] = basename($v, '.php');
            }
        }
        $base = get_class_methods('app\common\Base');
        $login = get_class_methods('app\admin\controller\Login');
        $filtration = array_merge($base, $login);
        foreach ($controllerList as $k => $v) {
            $arr = get_class_methods('app\admin\controller' . '\\' . $v);
            $actions[$v] = array_diff($arr, $filtration);
        }
        $controller = [];
        foreach ($controllerList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controller[] = ['name' => $this->get_class_annotation($v, 'admin'), 'controller' => $v];
            }
        }
        // dump($controller);
        View::assign('controller', $controller);
        if (request()->isGet()) {
            $act = Request::param('actions');
            if (isset($act) && $act != '') {
                foreach ($actions as $k => $v) {
                    if ($k == $act) {
                        // $arrs[] = ['name'=>$this->get_annotation('admin',$act,$v),'actions'=>$v];
                        foreach ($v as $k1 => $v1) {
                            $arrs[] = ['name' => $this->get_annotation('admin', $act, $v1), 'actions' => $v1];
                        }
                    }
                }
                return getBack(0, '', $arrs);
            }
        }
        $list = Menus::list(); //菜单列表
        View::assign('list', grades($list));
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(MenusVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            $res = Menus::add($param);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 菜单编辑
     */
    public function edit()
    {
        $path = base_path() . 'admin' . DIRECTORY_SEPARATOR . 'controller'; //控制器路径
        $pathList = glob($path . DIRECTORY_SEPARATOR . '*.php'); //模糊查询匹配
        $controllerList = [];
        foreach ($pathList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controllerList[] = basename($v, '.php');
            }
        }
        $base = get_class_methods('app\common\Base');
        $base = get_class_methods('app\common\Base');
        $login = get_class_methods('app\Admin\controller\Login');
        $filtration = array_merge($base, $login);
        foreach ($controllerList as $k => $v) {
            $arr = get_class_methods('app\admin\controller' . '\\' . $v);
            $actions[$v] = array_diff($arr, $filtration);
        }
        $controller = [];
        foreach ($controllerList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controller[] = ['name' => $this->get_class_annotation($v, 'admin'), 'controller' => $v];
            }
        }
        View::assign('controller', $controller);
        if (request()->isGet()) {
            $act = Request::param('actions');
            $id = Request::param('id');
            if (isset($act) && $act != '') {
                foreach ($actions as $k => $v) {
                    if ($k == $act) {
                        // $arr = $v;
                        foreach ($v as $k1 => $v1) {
                            $arrs[] = ['name' => $this->get_annotation('admin', $act, $v1), 'actions' => $v1];
                        }
                    }
                }
                return getBack(0, '', $arrs);
            }
            if (isset($id)) {
                $find = Menus::find($id);
                View::assign('find', $find);
            }
            $list = Menus::list([['id', 'not in', $id]]); //菜单列表
            View::assign('list', grades($list));
        }

        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(MenusVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            $res = Menus::edit($param);

            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 菜单删除
     */
    public function del()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $res = Menus::del($id);
            if ($res == "success") {
                return getBack(0);
            } else {
                return getBack(3, $res);
            }
        }
    }
}
